<?php
/**
 * 毛文科
 */
namespace app\admin\builder\bind;

use app\admin\model\getdatas\LianBiao;

class Form{
    protected $data = [];
    protected $action = '';
    protected $button = [];

    /**设置提交的api接口
     * @param string $action 接口
     * @return $this
     */
    public function setAction($action=''){
        $this->action = url($action);
        return $this;
    }
    /**添加设置的form对象
     * @param form对象的类型[input,checkbox,editor,file,files,img,imgs,none,password,radio,select,text,textarea,time,switch] $type
     * @param string字段名称 $name
     * @param string字段描述 $desc
     * @param string字段值 $value
     * @param array数据 $option
     * @param bool数据是否id对应值 $isIdtoName
     * @param string是否有ajaxapi $action
     * @param stringstring样式设置 $class 样式设置
     * @param string样式设置 $style 样式设置
     */
    public function setProject($type='input',$name='', $desc='描述',$value='',$option='开启,关闭',$describe='',$isIdtoName=false,$action='', $class='', $style='',$table=null){
        if($type!=='inclass'){
            if(!is_array($option)){
//                $option = explode(',',$option);
                $option = strToArray($option,',');
            }
            if($type=='checkbox'||$type=='tree'){
                if($value){
//                    $value = explode(',',$value);
                    $value = strToArray($value,',');
                }else{
                    $value = [];
                }
                if($type=='tree'){
                    $value = json_encode($value,JSON_UNESCAPED_UNICODE);
                    $option = json_encode($option,JSON_UNESCAPED_UNICODE);
                }
            }
            if($type=='switch'){
                foreach ($option as $key=>$values){
                    $option[$key] = getTranslate($values);
                }
            }
        }else{
            if($value){
                $objs = new LianBiao();;
                $objs->setModelId($table);
                $newvalue = $objs->getPidAll($value);
                $data['newvalue'] = implode(',',$newvalue);
            }
        }
        $data['type'] = $type;
        $data['name'] = $name;
        $data['desc'] = getTranslate($desc);
        $data['value'] = $value;
        $data['option'] = $option;
        $data['action'] = url($action);
        $data['isidtoname'] = $isIdtoName;
        $data['class'] = $class;
        $data['style'] = $style;
        $data['describe'] = getTranslate($describe);
        $this->data[] = $data;
        return $this;
    }

    /**设置按钮
     * @param string $type 按钮类型[back,submit]
     * @param string $desc 描述
     * @param string $class 样式设置
     * @param string $style 样式设置
     * @return $this
     */
    public function setButton($type='back',$desc='返回',$class=null,$style=null){
        $data['type'] = $type;
        $data['desc'] = getTranslate($desc);
        $data['class'] = $class;
        $data['style'] = $style;
        $this->button[] = $data;
        return $this;
    }

    /**加载form表单
     * @return \think\response\View
     */
    public function fetch(){
        if(request()->controller()=='Index'){
            return view('index/form',['data'=>$this->data,'action'=>$this->action,'button'=>$this->button]);
        }
        $qinius = isQiNiu();
        return view('index/form',['data'=>$this->data,'action'=>$this->action,'button'=>$this->button,'isQiniu'=>$qinius]);
    }
}